package com.yanqu.road.server.logic.activity.condition.share;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.ShareInvitationData;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.task.TaskModule;
import com.yanqu.road.server.logic.activity.BaseActivityData;
import com.yanqu.road.server.logic.activity.condition.BaseActivityCondition;
import com.yanqu.road.server.manger.activity.share.ShareMgr;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.List;
import java.util.Objects;

public class ShareNumActivityCondition extends BaseActivityCondition {
    public ShareNumActivityCondition(ActivityInfo activityInfo, ActivityConditionInfo activityConditionInfo, BaseActivityData baseActivityData) {
        super(activityInfo, activityConditionInfo, baseActivityData);
    }

    @Override
    protected void player_BoardLevelHandle(Object object) {
        List<ShareInvitationData> dataList = ShareMgr.getShareInvitationDataList(player.getUserId());
        BigInteger curValue = BigInteger.valueOf(dataList.size());
        if (!Objects.equals(curValue, getValue())) {
            setParam(String.valueOf(dataList.size()));
            setValue(curValue);
        }
    }


    @Override
    public void addTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().addListener(eGamePlayerEventType.ShareNum.getValue(), handle);

    }

    @Override
    public void removeTrigger(GamePlayer player) {
        player.getModule(TaskModule.class).getGameEvent().removeListener(eGamePlayerEventType.ShareNum.getValue(), handle);
    }

    @Override
    public boolean isCompleted(GamePlayer player) {
        List<ShareInvitationData> dataList = ShareMgr.getShareInvitationDataList(player.getUserId());
        int maxTimes = activityConditionInfo.getParamList().get(3).intValue();
        return dataList.size() >= maxTimes;
    }

    @Override
    public void initPreValue() {
        String param = getParam();
        if (StringUtils.isNullOrEmpty(param)) {
            List<ShareInvitationData> dataList = ShareMgr.getShareInvitationDataList(player.getUserId());
            setParam(String.valueOf(dataList.size()));
            setValue(BigInteger.valueOf(dataList.size()));
        }
    }
}
